package com.it.machine.controller;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.it.common.annotation.Log;
import com.it.common.core.controller.BaseController;
import com.it.common.core.domain.AjaxResult;
import com.it.common.core.page.TableDataInfo;
import com.it.common.enums.BusinessType;
import com.it.common.utils.poi.ExcelUtil;
import com.it.machine.domain.ActivationCode;
import com.it.machine.service.IActivationCodeService;

import jakarta.servlet.http.HttpServletResponse;


/**
 * 激活码Controller
 * 
 * @author 云淡风轻
 * @date 2025-05-08
 */
@RestController
@RequestMapping("/machine/code")
public class ActivationCodeController extends BaseController
{
    @Autowired
    private IActivationCodeService activationCodeService;

    /**
     * 查询激活码列表
     */
    @PreAuthorize("@ss.hasPermi('machine:code:list')")
    @GetMapping("/list")
    public TableDataInfo list(ActivationCode activationCode)
    {
        startPage();
        List<ActivationCode> list = activationCodeService.selectActivationCodeList(activationCode);
        return getDataTable(list);
    }

    /**
     * 导出激活码列表
     */
    @PreAuthorize("@ss.hasPermi('machine:code:export')")
    @Log(title = "激活码", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ActivationCode activationCode)
    {
        List<ActivationCode> list = activationCodeService.selectActivationCodeList(activationCode);
        ExcelUtil<ActivationCode> util = new ExcelUtil<ActivationCode>(ActivationCode.class);
        util.exportExcel(response, list, "激活码数据");
    }

    /**
     * 获取激活码详细信息
     */
    @PreAuthorize("@ss.hasPermi('machine:code:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(activationCodeService.selectActivationCodeById(id));
    }

    /**
     * 新增激活码
     */
    @PreAuthorize("@ss.hasPermi('machine:code:add')")
    @Log(title = "激活码", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ActivationCode activationCode)
    {
        return toAjax(activationCodeService.insertActivationCode(activationCode));
    }

    /**
     * 修改激活码
     */
    @PreAuthorize("@ss.hasPermi('machine:code:edit')")
    @Log(title = "激活码", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ActivationCode activationCode)
    {
        return toAjax(activationCodeService.updateActivationCode(activationCode));
    }

    /**
     * 删除激活码
     */
    @PreAuthorize("@ss.hasPermi('machine:code:remove')")
    @Log(title = "激活码", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(activationCodeService.deleteActivationCodeByIds(ids));
    }

    /**
     * 生成激活码
     */
    @PreAuthorize("@ss.hasPermi('machine:code:generate')")
    @Log(title = "激活码", businessType = BusinessType.INSERT)
    @PostMapping("/generate")
    public AjaxResult generateActiveCodes(@RequestBody GenerateActiveCodeRequest request)
    {
        List<ActivationCode> activationList = activationCodeService.generateActivationCodes(
            request.getCount(), 
            request.getProId(),
            request.getProName(),
            request.getMark(),
            request.getValidityType(),
            request.getValidityValue()
        );
        return success(activationList);
    }

    /**
     * 生成激活码请求参数
     */
    static class GenerateActiveCodeRequest {
        private int count; // 生成数量
        private String proId; // 项目ID
        private String proName; // 项目名称
        private Long mark; // 积分
        private String validityType; // 有效期类型
        private Long validityValue; // 有效期值
        
        public int getCount() {
            return count;
        }
        
        public void setCount(int count) {
            this.count = count;
        }
        
        public String getProId() {
            return proId;
        }
        
        public void setProId(String proId) {
            this.proId = proId;
        }
        
        public String getProName() {
            return proName;
        }
        
        public void setProName(String proName) {
            this.proName = proName;
        }
        
        public Long getMark() {
            return mark;
        }
        
        public void setMark(Long mark) {
            this.mark = mark;
        }
        
        public String getValidityType() {
            return validityType;
        }
        
        public void setValidityType(String validityType) {
            this.validityType = validityType;
        }
        
        public Long getValidityValue() {
            return validityValue;
        }
        
        public void setValidityValue(Long validityValue) {
            this.validityValue = validityValue;
        }
    }
    
    /**
     * 分配激活码给用户
     */
    @PreAuthorize("@ss.hasPermi('machine:code:allocate')")
    @Log(title = "激活码分配", businessType = BusinessType.UPDATE)
    @PostMapping("/allocate")
    public AjaxResult allocateActivationCodes(@RequestBody AllocateActivationCodeRequest request) {
        return toAjax(activationCodeService.allocateActivationCodesToUser(request.getIds(), request.getAllocateUserId(), request.getAllocateUserName()));
    }
    
    /**
     * 批量分配激活码给用户
     */
    @PreAuthorize("@ss.hasPermi('machine:code:allocate')")
    @Log(title = "激活码批量分配", businessType = BusinessType.UPDATE)
    @PostMapping("/batchAllocate")
    public AjaxResult batchAllocateActivationCodes(@RequestBody BatchAllocateActivationCodeRequest request) {
        List<ActivationCode> codes = activationCodeService.batchAllocateActivationCodes(request.getProId(), request.getCount(), request.getAllocateUserId(), request.getAllocateUserName());
        return success(codes);
    }
    
    /**
     * 分配激活码请求参数
     */
    static class AllocateActivationCodeRequest {
        private Long[] ids;
        private Long allocateUserId;
        private String allocateUserName;
        
        public Long[] getIds() {
            return ids;
        }
        
        public void setIds(Long[] ids) {
            this.ids = ids;
        }
        
        public Long getAllocateUserId() {
            return allocateUserId;
        }
        
        public void setAllocateUserId(Long allocateUserId) {
            this.allocateUserId = allocateUserId;
        }
        
        public String getAllocateUserName() {
            return allocateUserName;
        }
        
        public void setAllocateUserName(String allocateUserName) {
            this.allocateUserName = allocateUserName;
        }
    }
    
    /**
     * 批量分配激活码请求参数
     */
    static class BatchAllocateActivationCodeRequest {
        private String proId;
        private int count;
        private Long allocateUserId;
        private String allocateUserName;
        
        public String getProId() {
            return proId;
        }
        
        public void setProId(String proId) {
            this.proId = proId;
        }
        
        public int getCount() {
            return count;
        }
        
        public void setCount(int count) {
            this.count = count;
        }
        
        public Long getAllocateUserId() {
            return allocateUserId;
        }
        
        public void setAllocateUserId(Long allocateUserId) {
            this.allocateUserId = allocateUserId;
        }
        
        public String getAllocateUserName() {
            return allocateUserName;
        }
        
        public void setAllocateUserName(String allocateUserName) {
            this.allocateUserName = allocateUserName;
        }
    }
}